//
//  CoverViewController.m
//  Menu
//
//  Created by trimad on 11-11-11.
//  Copyright 2011 Trimad Group. All rights reserved.
//

#import <QuartzCore/QuartzCore.h>
#import "CoverViewController.h"
#import "MenuViewController.h"
#import "OrderInHandViewController.h"
#import "ManagementViewController.h"
#import "ContextManager.h"

@interface CoverViewController(Private) 
-(void) handleTapGestures:(UIGestureRecognizer *) sender;
@end

@implementation CoverViewController

@synthesize coverImageView;
@synthesize hintLable;
@synthesize orderButton;
@synthesize orderInHandView;

MenuViewController *menuViewController; 

- (void)viewDidLoad {
	hintLable.text = @"双击进入菜单";
	// double tap to open Pages.
	UITapGestureRecognizer *tapToOpenGesture = 
	[[UITapGestureRecognizer alloc] initWithTarget:self 
											action:@selector(handleTapGestures:)];
	tapToOpenGesture.numberOfTapsRequired = 2;
	[coverImageView addGestureRecognizer:tapToOpenGesture];
	
	[tapToOpenGesture release];
	orderButton.hidden = YES;
    [super viewDidLoad];
}

-(void) handleTapGestures:(UIGestureRecognizer *) sender {
	ContextManager *contextManager=[ContextManager getInstance];
	if (contextManager.rootViewController == nil) {
		menuViewController = [[MenuViewController alloc] 
							  initWithNibName: @"MenuViewController" 
							  bundle:nil];
		contextManager.rootViewController = menuViewController;
	}
	orderButton.hidden = NO;
	
	CATransition *transition = [CATransition animation];
	transition.duration = 1.0f;
	transition.timingFunction = [CAMediaTimingFunction 
								 functionWithName:kCAMediaTimingFunctionEaseOut];
	transition.type = @"pageCurl";
	if (self.interfaceOrientation == UIInterfaceOrientationPortrait) {
		transition.subtype = kCATransitionFromRight;
	} else {
		transition.subtype = kCATransitionFromLeft;
	}
	transition.delegate = self;
	[self.view.layer removeAllAnimations];
	[self.view.layer addAnimation:transition forKey:@"pageFlippingAnimation"];
	
	[self.view addSubview:contextManager.rootViewController.view];
	
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationPortrait || 
			interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}

- (void)viewWillAppear:(BOOL)animated {
	[super viewWillAppear:animated];
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}


- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[menuViewController release];
	[coverImageView release];
	[hintLable release];
    [super dealloc];
}

-(IBAction)orderSummaryClicked:(id)sender {
	OrderInHandViewController *orderInHandViewController = [[OrderInHandViewController alloc] 
															initWithNibName:@"OrderInHandViewController" bundle:nil];
	orderInHandView.hidden = NO;
	[orderInHandView addSubview: orderInHandViewController.view];
}

- (IBAction)managementButtonClicked:(id)sender {
	ManagementViewController *viewController = [[ManagementViewController alloc] 
														initWithNibName:@"ManagementViewController" bundle:nil];
	[self.view addSubview: viewController.view];
}


@end
